<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * App\Models\Broadcast
 *
 * @property int $id
 * @property int $admin_user_id who published broadcast
 * @property string|null $type
 * @property string|null $message
 * @property array|null $data data
 * @property \Illuminate\Support\Carbon|null $expired_at
 * @property int $is_closed
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read Administrator $adminUser
 * @method static \Database\Factories\BroadcastFactory factory(...$parameters)
 * @method static Builder|Broadcast newModelQuery()
 * @method static Builder|Broadcast newQuery()
 * @method static Builder|Broadcast query()
 * @method static Builder|Broadcast unclosed()
 * @method static Builder|Broadcast unexpired()
 * @method static Builder|Broadcast whereAdminUserId($value)
 * @method static Builder|Broadcast whereCreatedAt($value)
 * @method static Builder|Broadcast whereData($value)
 * @method static Builder|Broadcast whereExpiredAt($value)
 * @method static Builder|Broadcast whereId($value)
 * @method static Builder|Broadcast whereIsClosed($value)
 * @method static Builder|Broadcast whereMessage($value)
 * @method static Builder|Broadcast whereType($value)
 * @method static Builder|Broadcast whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Broadcast extends Model
{
    use HasFactory;
    
    protected $guarded = ['id'];
    
    protected $casts = [
    	'data' => 'json'
    ];
    
    protected $dates = [
    	'expired_at'
    ];
    
    public function scopeUnclosed(Builder $builder)
    {
	    return $builder->where('is_closed', false);
    }
    
    public function scopeUnexpired(Builder $builder)
    {
    	return $builder->whereDate('expired_at', '>', Carbon::now()->toDateTimeString());
    }
    
    public function adminUser()
    {
    	return $this->belongsTo(Administrator::class, 'admin_user_id', 'id');
    }
}
